package com.wang.web3;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wang.web3.base.Constant;
import com.wang.web3.bean.TianWallet;
import com.wang.web3.bean.TianWallets;
import com.wang.web3.utils.SPUtils;

import org.web3j.crypto.CipherException;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.Keys;
import org.web3j.crypto.Wallet;
import org.web3j.crypto.WalletFile;
import org.web3j.crypto.WalletUtils;
import org.web3j.utils.Numeric;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import static org.web3j.crypto.Wallet.decrypt;

public class IntoStoreFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    Gson gson = new Gson();

    public IntoStoreFragment() {
        // Required empty public constructor
    }

    EditText etStores,etpwd;
    Button mBtn;
    ECKeyPair ecKeyPair;

    public static IntoStoreFragment newInstance() {
        IntoStoreFragment fragment = new IntoStoreFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_into_store, container, false);
        etStores = view.findViewById(R.id.etStores);
        etpwd = view.findViewById(R.id.walletPwd);
        mBtn = view.findViewById(R.id.mBtnInto);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                creatWallet();
            }
        });
        return view;
    }

    private void creatWallet() {
        String store = etStores.getText().toString();
        String pwd = etpwd.getText().toString();
        if (store.equals("") || pwd.equals("")){
            Toast.makeText(getActivity(),"以上信息必填",Toast.LENGTH_SHORT).show();
            return;
        }
        WalletFile walletFile = gson.fromJson(store,WalletFile.class);

        try {
            ecKeyPair =  Wallet.decrypt(pwd,walletFile);
//            String filePath = Environment.getExternalStorageDirectory().getAbsolutePath().toString() + Constant.WalletUri;
//            String fileName =  WalletUtils.generateWalletFile(pwd,ecKeyPair,new File(filePath),false);
        } catch (CipherException e) {
            e.printStackTrace();
        }


        String address = Keys.getAddress(ecKeyPair);

        TianWallet tianWallet = new TianWallet("","0x" + address, Numeric.toHexStringWithPrefix(ecKeyPair.getPrivateKey()), Numeric.toHexStringWithPrefix(ecKeyPair.getPublicKey()), "");

        Gson gson = new Gson();
        //读出原有的
        String ss = (String) SPUtils.get(getActivity(), Constant.WalletKey,"");
        TianWallets wallets;
        if (!ss.equals("")){
            wallets = gson.fromJson(ss, TianWallets.class);
        }else {
            wallets = new TianWallets(new ArrayList<TianWallet>());
        }

        wallets.getTianWallets().add(tianWallet);
        SPUtils.put(getActivity(), Constant.WalletKey,gson.toJson(wallets));


        Toast.makeText(getActivity(),"导入成功",Toast.LENGTH_SHORT).show();
        getActivity().setResult(1,new Intent());
        getActivity().finish();
    }


}
